﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Windows.Controls;
using NumericalData;

namespace WPFSolution.CommonOperations
{
    public class FileOperations
    {
        private static NumberStyles DoubleStyle = NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent | NumberStyles.AllowParentheses;


        public static void LoadMatrixAndVectorFromFile(string path, ref SquareMatrix matr, ref Vector vec)
        {
            using (StreamReader stream = File.OpenText(path))
            {
                var separators = new[] { " ", "\t", ";" };
                var matrDim = 0;
                var vecDim = 0;
                var matrArray = ReadMatrixFromFile(stream, separators, out matrDim);
                stream.ReadLine();
                var vecArray = ReadVectorFromFile(stream, separators, out vecDim);
                if (vecDim != matrDim)
                    throw new Exception("Objects Have Different Dimentions");
                matr = new SquareMatrix(matrArray);
                vec = new Vector(vecArray);
            }
        }

        static private double[][] ReadMatrixFromFile(StreamReader stream, string[] separators, out int dimention)
        {
            var firstRow = stream.ReadLine().Split(separators, StringSplitOptions.None);
            dimention = firstRow.Length;
            var rows = new List<double[]>(dimention);
            var bufferDoubleList = new List<double>(dimention);
            foreach (var elem in firstRow)
            {
                bufferDoubleList.Add(ParseStringToDouble(elem));
            }
            rows.Add(bufferDoubleList.ToArray());
            for (int i = 1; i <= dimention - 1; ++i)
            {
                var stringRow = stream.ReadLine().Split(separators, StringSplitOptions.None);
                if (stringRow.Length != dimention)
                    throw new Exception("Objects Have Different Dimentions");
                var buffer = new List<double>(dimention);
                foreach (var elem in stringRow)
                {
                    buffer.Add(ParseStringToDouble(elem));
                }
                rows.Add(buffer.ToArray());
            }
            return rows.ToArray();
        }

        static private double[] ReadVectorFromFile(StreamReader stream, string[] separators, out int dimention)
        {
            var stringRow = stream.ReadLine().Split(separators, StringSplitOptions.None);
            dimention = stringRow.Length;
            var bufferDoubleList = new List<double>(dimention);
            foreach (var elem in stringRow)
            {
                bufferDoubleList.Add(ParseStringToDouble(elem));
            }
            return bufferDoubleList.ToArray();
        }

        static Double ParseStringToDouble(string input)
        {
            return Double.Parse(input, DoubleStyle);
        }
    }
}
